home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb-4.5 / dist / gdb / infcmd.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-02  |  32.6 KB  |  1,232 lines

  1. /* Memory-access and commands for "inferior" (child) process, for GDB.
  2.    Copyright 1986, 1987, 1988, 1989, 1991, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include "defs.h"
  21. #include <signal.h>
  22. #include <sys/param.h>
  23. #include <string.h>
  24. #include "symtab.h"
  25. #include "gdbtypes.h"
  26. #include "frame.h"
  27. #include "inferior.h"
  28. #include "environ.h"
  29. #include "value.h"
  30. #include "gdbcmd.h"
  31. #include "gdbcore.h"
  32. #include "target.h"
  33.  
  34. static void
  35. continue_command PARAMS ((char *, int));
  36.  
  37. static void
  38. until_next_command PARAMS ((int));
  39.  
  40. static void 
  41. until_command PARAMS ((char *, int));
  42.  
  43. static void
  44. path_info PARAMS ((char *, int));
  45.  
  46. static void
  47. path_command PARAMS ((char *, int));
  48.  
  49. static void
  50. unset_command PARAMS ((char *, int));
  51.  
  52. static void
  53. float_info PARAMS ((char *, int));
  54.  
  55. static void
  56. detach_command PARAMS ((char *, int));
  57.  
  58. static void
  59. nofp_registers_info PARAMS ((char *, int));
  60.  
  61. static void
  62. all_registers_info PARAMS ((char *, int));
  63.  
  64. static void
  65. registers_info PARAMS ((char *, int));
  66.  
  67. static void
  68. do_registers_info PARAMS ((int, int));
  69.  
  70. static void
  71. unset_environment_command PARAMS ((char *, int));
  72.  
  73. static void
  74. set_environment_command PARAMS ((char *, int));
  75.  
  76. static void
  77. environment_info PARAMS ((char *, int));
  78.  
  79. static void
  80. program_info PARAMS ((char *, int));
  81.  
  82. static void
  83. finish_command PARAMS ((char *, int));
  84.  
  85. static void
  86. signal_command PARAMS ((char *, int));
  87.  
  88. static void
  89. jump_command PARAMS ((char *, int));
  90.  
  91. static void
  92. step_1 PARAMS ((int, int, char *));
  93.  
  94. static void
  95. nexti_command PARAMS ((char *, int));
  96.  
  97. static void
  98. stepi_command PARAMS ((char *, int));
  99.  
  100. static void
  101. next_command PARAMS ((char *, int));
  102.  
  103. static void
  104. step_command PARAMS ((char *, int));
  105.  
  106. static void
  107. run_command PARAMS ((char *, int));
  108.  
  109. extern char *sys_siglist[];
  110.  
  111. #define ERROR_NO_INFERIOR \
  112.    if (!target_has_execution) error ("The program is not being run.");
  113.  
  114. /* String containing arguments to give to the program, separated by spaces.
  115.    Empty string (pointer to '\0') means no args.  */
  116.  
  117. static char *inferior_args;
  118.  
  119. /* File name for default use for standard in/out in the inferior.  */
  120.  
  121. char *inferior_io_terminal;
  122.  
  123. /* Pid of our debugged inferior, or 0 if no inferior now.
  124.    Since various parts of infrun.c test this to see whether there is a program
  125.    being debugged it should be nonzero (currently 3 is used) for remote
  126.    debugging.  */
  127.  
  128. int inferior_pid;
  129.  
  130. /* Last signal that the inferior received (why it stopped).  */
  131.  
  132. int stop_signal;
  133.  
  134. /* Address at which inferior stopped.  */
  135.  
  136. CORE_ADDR stop_pc;
  137.  
  138. /* Stack frame when program stopped.  */
  139.  
  140. FRAME_ADDR stop_frame_address;
  141.  
  142. /* Chain containing status of breakpoint(s) that we have stopped at.  */
  143.  
  144. bpstat stop_bpstat;
  145.  
  146. /* Flag indicating that a command has proceeded the inferior past the
  147.    current breakpoint.  */
  148.  
  149. int breakpoint_proceeded;
  150.  
  151. /* Nonzero if stopped due to a step command.  */
  152.  
  153. int stop_step;
  154.  
  155. /* Nonzero if stopped due to completion of a stack dummy routine.  */
  156.  
  157. int stop_stack_dummy;
  158.  
  159. /* Nonzero if stopped due to a random (unexpected) signal in inferior
  160.    process.  */
  161.  
  162. int stopped_by_random_signal;
  163.  
  164. /* Range to single step within.
  165.    If this is nonzero, respond to a single-step signal
  166.    by continuing to step if the pc is in this range.  */
  167.  
  168. CORE_ADDR step_range_start; /* Inclusive */
  169. CORE_ADDR step_range_end; /* Exclusive */
  170.  
  171. /* Stack frame address as of when stepping command was issued.
  172.    This is how we know when we step into a subroutine call,
  173.    and how to set the frame for the breakpoint used to step out.  */
  174.  
  175. FRAME_ADDR step_frame_address;
  176.  
  177. /* 1 means step over all subroutine calls.
  178.    -1 means step over calls to undebuggable functions.  */
  179.  
  180. int step_over_calls;
  181.  
  182. /* If stepping, nonzero means step count is > 1
  183.    so don't print frame next time inferior stops
  184.    if it stops due to stepping.  */
  185.  
  186. int step_multi;
  187.  
  188. /* Environment to use for running inferior,
  189.    in format described in environ.h.  */
  190.  
  191. struct environ *inferior_environ;
  192.  
  193.  
  194. /* ARGSUSED */
  195. void
  196. tty_command (file, from_tty)
  197.      char *file;
  198.      int from_tty;
  199. {
  200.   if (file == 0)
  201.     error_no_arg ("terminal name for running target process");
  202.  
  203.   inferior_io_terminal = savestring (file, strlen (file));
  204. }
  205.  
  206. static void
  207. run_command (args, from_tty)
  208.      char *args;
  209.      int from_tty;
  210. {
  211.   char *exec_file;
  212.  
  213.   dont_repeat ();
  214.  
  215.   if (inferior_pid)
  216.     {
  217.       if (
  218.       !query ("The program being debugged has been started already.\n\
  219. Start it from the beginning? "))
  220.     error ("Program not restarted.");
  221.       target_kill ();
  222.     }
  223.  
  224.   exec_file = (char *) get_exec_file (0);
  225.  
  226.   /* The exec file is re-read every time we do a generic_mourn_inferior, so
  227.      we just have to worry about the symbol file.  */
  228.   reread_symbols ();
  229.  
  230.   if (args)
  231.     {
  232.       char *cmd;
  233.       cmd = concat ("set args ", args, NULL);
  234.       make_cleanup (free, cmd);
  235.       execute_command (cmd, from_tty);
  236.     }
  237.  
  238.   if (from_tty)
  239.     {
  240.       printf ("Starting program: %s %s\n",
  241.           exec_file? exec_file: "", inferior_args);
  242.       fflush (stdout);
  243.     }
  244.  
  245.   target_create_inferior (exec_file, inferior_args,
  246.               environ_vector (inferior_environ));
  247. }
  248.  
  249. static void
  250. continue_command (proc_count_exp, from_tty)
  251.      char *proc_count_exp;
  252.      int from_tty;
  253. {
  254.   ERROR_NO_INFERIOR;
  255.  
  256.   /* If have argument, set proceed count of breakpoint we stopped at.  */
  257.  
  258.   if (proc_count_exp != NULL)
  259.     {
  260.       bpstat bs = stop_bpstat;
  261.       int num = bpstat_num (&bs);
  262.       if (num == 0 && from_tty)
  263.     {
  264.       printf_filtered
  265.         ("Not stopped at any breakpoint; argument ignored.\n");
  266.     }
  267.       while (num != 0)
  268.     {
  269.       set_ignore_count (num,
  270.                 parse_and_eval_address (proc_count_exp) - 1,
  271.                 from_tty);
  272.       /* set_ignore_count prints a message ending with a period.
  273.          So print two spaces before "Continuing.".  */
  274.       if (from_tty)
  275.         printf ("  ");
  276.       num = bpstat_num (&bs);
  277.     }
  278.     }
  279.  
  280.   if (from_tty)
  281.     printf ("Continuing.\n");
  282.  
  283.   clear_proceed_status ();
  284.  
  285.   proceed ((CORE_ADDR) -1, -1, 0);
  286. }
  287.  
  288. /* Step until outside of current statement.  */
  289.  
  290. /* ARGSUSED */
  291. static void
  292. step_command (count_string, from_tty)
  293.      char *count_string;
  294.      int from_tty;
  295. {
  296.   step_1 (0, 0, count_string);
  297. }
  298.  
  299. /* Likewise, but skip over subroutine calls as if single instructions.  */
  300.  
  301. /* ARGSUSED */
  302. static void
  303. next_command (count_string, from_tty)
  304.      char *count_string;
  305.      int from_tty;
  306. {
  307.   step_1 (1, 0, count_string);
  308. }
  309.  
  310. /* Likewise, but step only one instruction.  */
  311.  
  312. /* ARGSUSED */
  313. static void
  314. stepi_command (count_string, from_tty)
  315.      char *count_string;
  316.      int from_tty;
  317. {
  318.   step_1 (0, 1, count_string);
  319. }
  320.  
  321. /* ARGSUSED */
  322. static void
  323. nexti_command (count_string, from_tty)
  324.      char *count_string;
  325.      int from_tty;
  326. {
  327.   step_1 (1, 1, count_string);
  328. }
  329.  
  330. static void
  331. step_1 (skip_subroutines, single_inst, count_string)
  332.      int skip_subroutines;
  333.      int single_inst;
  334.      char *count_string;
  335. {
  336.   register int count = 1;
  337.   FRAME fr;
  338.   struct cleanup *cleanups = 0;
  339.  
  340.   ERROR_NO_INFERIOR;
  341.   count = count_string ? parse_and_eval_address (count_string) : 1;
  342.  
  343.   if (!single_inst || skip_subroutines) /* leave si command alone */
  344.     {
  345.       enable_longjmp_breakpoint();
  346.       cleanups = make_cleanup(disable_longjmp_breakpoint, 0);
  347.     }
  348.  
  349.   for (; count > 0; count--)
  350.     {
  351.       clear_proceed_status ();
  352.  
  353.       fr = get_current_frame ();
  354.       if (!fr)                /* Avoid coredump here.  Why tho? */
  355.     error ("No current frame");
  356.       step_frame_address = FRAME_FP (fr);
  357.  
  358.       if (! single_inst)
  359.     {
  360.       find_pc_line_pc_range (stop_pc, &step_range_start, &step_range_end);
  361.       if (step_range_end == 0)
  362.         {
  363.           struct minimal_symbol *msymbol;
  364.  
  365.           msymbol = lookup_minimal_symbol_by_pc (stop_pc);
  366.           target_terminal_ours ();
  367.           printf ("Current function has no line number information.\n");
  368.           fflush (stdout);
  369.  
  370.           /* No info or after _etext ("Can't happen") */
  371.           if (msymbol == NULL || (msymbol + 1) -> name == NULL)
  372.         error ("No data available on pc function.");
  373.  
  374.           printf ("Single stepping until function exit.\n");
  375.           fflush (stdout);
  376.  
  377.           step_range_start = msymbol -> address;
  378.           step_range_end = (msymbol + 1) -> address;
  379.         }
  380.     }
  381.       else
  382.     {
  383.       /* Say we are stepping, but stop after one insn whatever it does.
  384.          Don't step through subroutine calls even to undebuggable
  385.          functions.  */
  386.       step_range_start = step_range_end = 1;
  387.       if (!skip_subroutines)
  388.         step_over_calls = 0;
  389.     }
  390.  
  391.       if (skip_subroutines)
  392.     step_over_calls = 1;
  393.  
  394.       step_multi = (count > 1);
  395.       proceed ((CORE_ADDR) -1, -1, 1);
  396.       if (! stop_step)
  397.     break;
  398. #if defined (SHIFT_INST_REGS)
  399.       write_register (NNPC_REGNUM, read_register (NPC_REGNUM));
  400.       write_register (NPC_REGNUM, read_register (PC_REGNUM));
  401. #endif
  402.     }
  403.  
  404.   if (!single_inst || skip_subroutines)
  405.     do_cleanups(cleanups);
  406. }
  407.  
  408. /* Continue program at specified address.  */
  409.  
  410. static void
  411. jump_command (arg, from_tty)
  412.      char *arg;
  413.      int from_tty;
  414. {
  415.   register CORE_ADDR addr;
  416.   struct symtabs_and_lines sals;
  417.   struct symtab_and_line sal;
  418.  
  419.   ERROR_NO_INFERIOR;
  420.  
  421.   if (!arg)
  422.     error_no_arg ("starting address");
  423.  
  424.   sals = decode_line_spec_1 (arg, 1);
  425.   if (sals.nelts != 1)
  426.     {
  427.       error ("Unreasonable jump request");
  428.     }
  429.  
  430.   sal = sals.sals[0];
  431.   free ((PTR)sals.sals);
  432.  
  433.   if (sal.symtab == 0 && sal.pc == 0)
  434.     error ("No source file has been specified.");
  435.  
  436.   resolve_sal_pc (&sal);            /* May error out */
  437.  
  438.   {
  439.     struct symbol *fn = get_frame_function (get_current_frame ());
  440.     struct symbol *sfn = find_pc_function (sal.pc);
  441.     if (fn != 0 && sfn != fn
  442.     && ! query ("Line %d is not in `%s'.  Jump anyway? ",
  443.             sal.line, SYMBOL_NAME (fn)))
  444.       error ("Not confirmed.");
  445.   }
  446.  
  447.   addr = ADDR_BITS_SET (sal.pc);
  448.  
  449.   if (from_tty)
  450.     printf ("Continuing at %s.\n", local_hex_string(addr));
  451.  
  452.   clear_proceed_status ();
  453.   proceed (addr, 0, 0);
  454. }
  455.  
  456. /* Continue program giving it specified signal.  */
  457.  
  458. static void
  459. signal_command (signum_exp, from_tty)
  460.      char *signum_exp;
  461.      int from_tty;
  462. {
  463.   register int signum;
  464.  
  465.   dont_repeat ();        /* Too dangerous.  */
  466.   ERROR_NO_INFERIOR;
  467.  
  468.   if (!signum_exp)
  469.     error_no_arg ("signal number");
  470.  
  471.   signum = parse_and_eval_address (signum_exp);
  472.  
  473.   if (from_tty)
  474.     printf ("Continuing with signal %d.\n", signum);
  475.  
  476.   clear_proceed_status ();
  477.   proceed (stop_pc, signum, 0);
  478. }
  479.  
  480. /* Execute a "stack dummy", a piece of code stored in the stack
  481.    by the debugger to be executed in the inferior.
  482.  
  483.    To call: first, do PUSH_DUMMY_FRAME.
  484.    Then push the contents of the dummy.  It should end with a breakpoint insn.
  485.    Then call here, passing address at which to start the dummy.
  486.  
  487.    The contents of all registers are saved before the dummy frame is popped
  488.    and copied into the buffer BUFFER.
  489.  
  490.    The dummy's frame is automatically popped whenever that break is hit.
  491.    If that is the first time the program stops, run_stack_dummy
  492.    returns to its caller with that frame already gone.
  493.    Otherwise, the caller never gets returned to.  */
  494.  
  495. /* DEBUG HOOK:  4 => return instead of letting the stack dummy run.  */
  496.  
  497. static int stack_dummy_testing = 0;
  498.  
  499. void
  500. run_stack_dummy (addr, buffer)
  501.      CORE_ADDR addr;
  502.      char buffer[REGISTER_BYTES];
  503. {
  504.   /* Now proceed, having reached the desired place.  */
  505.   clear_proceed_status ();
  506.   if (stack_dummy_testing & 4)
  507.     {
  508.       POP_FRAME;
  509.       return;
  510.     }
  511.   proceed_to_finish = 1;    /* We want stop_registers, please... */
  512.   proceed (addr, 0, 0);
  513.  
  514.   if (!stop_stack_dummy)
  515.     /* This used to say
  516.        "Cannot continue previously requested operation".  */
  517.     error ("\
  518. The program being debugged stopped while in a function called from GDB.\n\
  519. The expression which contained the function call has been discarded.");
  520.  
  521.   /* On return, the stack dummy has been popped already.  */
  522.  
  523.   bcopy (stop_registers, buffer, sizeof stop_registers);
  524. }
  525.  
  526. /* Proceed until we reach a different source line with pc greater than
  527.    our current one or exit the function.  We skip calls in both cases.
  528.  
  529.    Note that eventually this command should probably be changed so
  530.    that only source lines are printed out when we hit the breakpoint
  531.    we set.  I'm going to postpone this until after a hopeful rewrite
  532.    of wait_for_inferior and the proceed status code. -- randy */
  533.  
  534. /* ARGSUSED */
  535. static void
  536. until_next_command (from_tty)
  537.      int from_tty;
  538. {
  539.   FRAME frame;
  540.   CORE_ADDR pc;
  541.   struct symbol *func;
  542.   struct symtab_and_line sal;
  543.  
  544.   clear_proceed_status ();
  545.  
  546.   frame = get_current_frame ();
  547.  
  548.   /* Step until either exited from this function or greater
  549.      than the current line (if in symbolic section) or pc (if
  550.      not). */
  551.  
  552.   pc = read_pc ();
  553.   func = find_pc_function (pc);
  554.   
  555.   if (!func)
  556.     {
  557.       struct minimal_symbol *msymbol = lookup_minimal_symbol_by_pc (pc);
  558.       
  559.       if (msymbol == NULL)
  560.     error ("Execution is not within a known function.");
  561.       
  562.       step_range_start = msymbol -> address;
  563.       step_range_end = pc;
  564.     }
  565.   else
  566.     {
  567.       sal = find_pc_line (pc, 0);
  568.       
  569.       step_range_start = BLOCK_START (SYMBOL_BLOCK_VALUE (func));
  570.       step_range_end = sal.end;
  571.     }
  572.   
  573.   step_over_calls = 1;
  574.   step_frame_address = FRAME_FP (frame);
  575.   
  576.   step_multi = 0;        /* Only one call to proceed */
  577.   
  578.   proceed ((CORE_ADDR) -1, -1, 1);
  579. }
  580.  
  581. static void 
  582. until_command (arg, from_tty)
  583.      char *arg;
  584.      int from_tty;
  585. {
  586.   if (!target_has_execution)
  587.     error ("The program is not running.");
  588.   if (arg)
  589.     until_break_command (arg, from_tty);
  590.   else
  591.     until_next_command (from_tty);
  592. }
  593.  
  594. /* "finish": Set a temporary breakpoint at the place
  595.    the selected frame will return to, then continue.  */
  596.  
  597. static void
  598. finish_command (arg, from_tty)
  599.      char *arg;
  600.      int from_tty;
  601. {
  602.   struct symtab_and_line sal;
  603.   register FRAME frame;
  604.   struct frame_info *fi;
  605.   register struct symbol *function;
  606.   struct breakpoint *breakpoint;
  607.   struct cleanup *old_chain;
  608.  
  609.   if (arg)
  610.     error ("The \"finish\" command does not take any arguments.");
  611.   if (!target_has_execution)
  612.     error ("The program is not running.");
  613.   if (selected_frame == NULL)
  614.     error ("No selected frame.");
  615.  
  616.   frame = get_prev_frame (selected_frame);
  617.   if (frame == 0)
  618.     error ("\"finish\" not meaningful in the outermost frame.");
  619.  
  620.   clear_proceed_status ();
  621.  
  622.   fi = get_frame_info (frame);
  623.   sal = find_pc_line (fi->pc, 0);
  624.   sal.pc = fi->pc;
  625.  
  626.   breakpoint = set_momentary_breakpoint (sal, frame, bp_finish);
  627.  
  628.   old_chain = make_cleanup(delete_breakpoint, breakpoint);
  629.  
  630.   /* Find the function we will return from.  */
  631.  
  632.   fi = get_frame_info (selected_frame);
  633.   function = find_pc_function (fi->pc);
  634.  
  635.   /* Print info on the selected frame, including level number
  636.      but not source.  */
  637.   if (from_tty)
  638.     {
  639.       printf_filtered ("Run till exit from ");
  640.       print_stack_frame (selected_frame, selected_frame_level, 0);
  641.     }
  642.  
  643.   proceed_to_finish = 1;        /* We want stop_registers, please... */
  644.   proceed ((CORE_ADDR) -1, -1, 0);
  645.  
  646.   /* Did we stop at our breakpoint? */
  647.   if (bpstat_find_breakpoint(stop_bpstat, breakpoint) != NULL
  648.       && function != 0)
  649.     {
  650.       struct type *value_type;
  651.       register value val;
  652.       CORE_ADDR funcaddr;
  653.  
  654.       value_type = TYPE_TARGET_TYPE (SYMBOL_TYPE (function));
  655.       if (!value_type)
  656.     fatal ("internal: finish_command: function has no target type");
  657.       
  658.       if (TYPE_CODE (value_type) == TYPE_CODE_VOID)
  659.     return;
  660.  
  661.       funcaddr = BLOCK_START (SYMBOL_BLOCK_VALUE (function));
  662.  
  663.       val = value_being_returned (value_type, stop_registers,
  664.           using_struct_return (value_of_variable (function),
  665.                    funcaddr,
  666.                    value_type,
  667.         BLOCK_GCC_COMPILED (SYMBOL_BLOCK_VALUE (function))));
  668.  
  669.       printf_filtered ("Value returned is $%d = ", record_latest_value (val));
  670.       value_print (val, stdout, 0, Val_no_prettyprint);
  671.       printf_filtered ("\n");
  672.     }
  673.   do_cleanups(old_chain);
  674. }
  675.  
  676. /* ARGSUSED */
  677. static void
  678. program_info (args, from_tty)
  679.     char *args;
  680.     int from_tty;
  681. {
  682.   bpstat bs = stop_bpstat;
  683.   int num = bpstat_num (&bs);
  684.   
  685.   if (!target_has_execution)
  686.     {
  687.       printf ("The program being debugged is not being run.\n");
  688.       return;
  689.     }
  690.  
  691.   target_files_info ();
  692.   printf ("Program stopped at %s.\n", local_hex_string(stop_pc));
  693.   if (stop_step)
  694.     printf ("It stopped after being stepped.\n");
  695.   else if (num != 0)
  696.     {
  697.       /* There may be several breakpoints in the same place, so this
  698.      isn't as strange as it seems.  */
  699.       while (num != 0)
  700.     {
  701.       if (num < 0)
  702.         printf ("It stopped at a breakpoint that has since been deleted.\n");
  703.       else
  704.         printf ("It stopped at breakpoint %d.\n", num);
  705.       num = bpstat_num (&bs);
  706.     }
  707.     }
  708.   else if (stop_signal) {
  709. #ifdef PRINT_RANDOM_SIGNAL
  710.     PRINT_RANDOM_SIGNAL (stop_signal);
  711. #else
  712.     printf ("It stopped with signal %d (%s).\n",
  713.         stop_signal, 
  714.         (stop_signal > NSIG)? "unknown": sys_siglist[stop_signal]);
  715. #endif
  716.   }
  717.  
  718.   if (!from_tty)
  719.     printf ("Type \"info stack\" or \"info registers\" for more information.\n");
  720. }
  721.  
  722. static void
  723. environment_info (var, from_tty)
  724.      char *var;
  725.      int from_tty;
  726. {
  727.   if (var)
  728.     {
  729.       register char *val = get_in_environ (inferior_environ, var);
  730.       if (val)
  731.     printf ("%s = %s\n", var, val);
  732.       else
  733.     printf ("Environment variable \"%s\" not defined.\n", var);
  734.     }
  735.   else
  736.     {
  737.       register char **vector = environ_vector (inferior_environ);
  738.       while (*vector)
  739.     printf ("%s\n", *vector++);
  740.     }
  741. }
  742.  
  743. static void
  744. set_environment_command (arg, from_tty)
  745.      char *arg;
  746.      int from_tty;
  747. {
  748.   register char *p, *val, *var;
  749.   int nullset = 0;
  750.  
  751.   if (arg == 0)
  752.     error_no_arg ("environment variable and value");
  753.  
  754.   /* Find seperation between variable name and value */
  755.   p = (char *) strchr (arg, '=');
  756.   val = (char *) strchr (arg, ' ');
  757.  
  758.   if (p != 0 && val != 0)
  759.     {
  760.       /* We have both a space and an equals.  If the space is before the
  761.      equals and the only thing between the two is more space, use
  762.      the equals */
  763.       if (p > val)
  764.     while (*val == ' ')
  765.       val++;
  766.  
  767.       /* Take the smaller of the two.  If there was space before the
  768.      "=", they will be the same right now. */
  769.       p = arg + min (p - arg, val - arg);
  770.     }
  771.   else if (val != 0 && p == 0)
  772.     p = val;
  773.  
  774.   if (p == arg)
  775.     error_no_arg ("environment variable to set");
  776.  
  777.   if (p == 0 || p[1] == 0)
  778.     {
  779.       nullset = 1;
  780.       if (p == 0)
  781.     p = arg + strlen (arg);    /* So that savestring below will work */
  782.     }
  783.   else
  784.     {
  785.       /* Not setting variable value to null */
  786.       val = p + 1;
  787.       while (*val == ' ' || *val == '\t')
  788.     val++;
  789.     }
  790.  
  791.   while (p != arg && (p[-1] == ' ' || p[-1] == '\t')) p--;
  792.  
  793.   var = savestring (arg, p - arg);
  794.   if (nullset)
  795.     {
  796.       printf ("Setting environment variable \"%s\" to null value.\n", var);
  797.       set_in_environ (inferior_environ, var, "");
  798.     }
  799.   else
  800.     set_in_environ (inferior_environ, var, val);
  801.   free (var);
  802. }
  803.  
  804. static void
  805. unset_environment_command (var, from_tty)
  806.      char *var;
  807.      int from_tty;
  808. {
  809.   if (var == 0)
  810.     {
  811.       /* If there is no argument, delete all environment variables.
  812.      Ask for confirmation if reading from the terminal.  */
  813.       if (!from_tty || query ("Delete all environment variables? "))
  814.     {
  815.       free_environ (inferior_environ);
  816.       inferior_environ = make_environ ();
  817.     }
  818.     }
  819.   else
  820.     unset_in_environ (inferior_environ, var);
  821. }
  822.  
  823. /* Handle the execution path (PATH variable) */
  824.  
  825. const static char path_var_name[] = "PATH";
  826.  
  827. /* ARGSUSED */
  828. static void
  829. path_info (args, from_tty)
  830.      char *args;
  831.      int from_tty;
  832. {
  833.   printf ("Executable and object file path: %s\n", 
  834.       get_in_environ (inferior_environ, path_var_name));
  835. }
  836.  
  837. /* Add zero or more directories to the front of the execution path.  */
  838.  
  839. static void
  840. path_command (dirname, from_tty)
  841.      char *dirname;
  842.      int from_tty;
  843. {
  844.   char *exec_path;
  845.  
  846.   dont_repeat ();
  847.   exec_path = strsave (get_in_environ (inferior_environ, path_var_name));
  848.   mod_path (dirname, &exec_path);
  849.   set_in_environ (inferior_environ, path_var_name, exec_path);
  850.   free (exec_path);
  851.   if (from_tty)
  852.     path_info ((char *)NULL, from_tty);
  853. }
  854.  
  855. CORE_ADDR
  856. read_pc ()
  857. {
  858.   return ADDR_BITS_REMOVE ((CORE_ADDR) read_register (PC_REGNUM));
  859. }
  860.  
  861. void
  862. write_pc (val)
  863.      CORE_ADDR val;
  864. {
  865.   write_register (PC_REGNUM, (long) val);
  866. #ifdef NPC_REGNUM
  867.   write_register (NPC_REGNUM, (long) val+4);
  868. #endif
  869.   pc_changed = 0;
  870. }
  871.  
  872. char *reg_names[] = REGISTER_NAMES;
  873.  
  874. /* Print out the machine register regnum. If regnum is -1,
  875.    print all registers (fpregs == 1) or all non-float registers
  876.    (fpregs == 0).
  877.  
  878.    For most machines, having all_registers_info() print the
  879.    register(s) one per line is good enough. If a different format
  880.    is required, (eg, for MIPS or Pyramid 90x, which both have
  881.    lots of regs), or there is an existing convention for showing
  882.    all the registers, define the macro DO_REGISTERS_INFO(regnum, fp)
  883.    to provide that format.  */  
  884.  
  885. #if !defined (DO_REGISTERS_INFO)
  886. #define DO_REGISTERS_INFO(regnum, fp) do_registers_info(regnum, fp)
  887. static void
  888. do_registers_info (regnum, fpregs)
  889.      int regnum;
  890.      int fpregs;
  891. {
  892.   register int i;
  893.  
  894.   for (i = 0; i < NUM_REGS; i++)
  895.     {
  896.       char raw_buffer[MAX_REGISTER_RAW_SIZE];
  897.       char virtual_buffer[MAX_REGISTER_VIRTUAL_SIZE];
  898.  
  899.       /* Decide between printing all regs, nonfloat regs, or specific reg.  */
  900.       if (regnum == -1) {
  901.     if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT && !fpregs)
  902.       continue;
  903.       } else {
  904.         if (i != regnum)
  905.       continue;
  906.       }
  907.  
  908.       fputs_filtered (reg_names[i], stdout);
  909.       print_spaces_filtered (15 - strlen (reg_names[i]), stdout);
  910.  
  911.       /* Get the data in raw format, then convert also to virtual format.  */
  912.       if (read_relative_register_raw_bytes (i, raw_buffer))
  913.     {
  914.       printf_filtered ("Invalid register contents\n");
  915.       continue;
  916.     }
  917.       
  918.       target_convert_to_virtual (i, raw_buffer, virtual_buffer);
  919.  
  920.       /* If virtual format is floating, print it that way, and in raw hex.  */
  921.       if (TYPE_CODE (REGISTER_VIRTUAL_TYPE (i)) == TYPE_CODE_FLT
  922.       && ! INVALID_FLOAT (virtual_buffer, REGISTER_VIRTUAL_SIZE (i)))
  923.     {
  924.       register int j;
  925.  
  926.       val_print (REGISTER_VIRTUAL_TYPE (i), virtual_buffer, 0,
  927.              stdout, 0, 1, 0, Val_pretty_default);
  928.  
  929.       printf_filtered ("\t(raw 0x");
  930.       for (j = 0; j < REGISTER_RAW_SIZE (i); j++)
  931.         printf_filtered ("%02x", (unsigned char)raw_buffer[j]);
  932.       printf_filtered (")");
  933.     }
  934.  
  935. /* FIXME!  val_print probably can handle all of these cases now...  */
  936.  
  937.       /* Else if virtual format is too long for printf,
  938.      print in hex a byte at a time.  */
  939.       else if (REGISTER_VIRTUAL_SIZE (i) > sizeof (long))
  940.     {
  941.       register int j;
  942.       printf_filtered ("0x");
  943.       for (j = 0; j < REGISTER_VIRTUAL_SIZE (i); j++)
  944.         printf_filtered ("%02x", (unsigned char)virtual_buffer[j]);
  945.     }
  946.       /* Else print as integer in hex and in decimal.  */
  947.       else
  948.     {
  949.       val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
  950.              stdout, 'x', 1, 0, Val_pretty_default);
  951.       printf_filtered ("\t");
  952.       val_print (REGISTER_VIRTUAL_TYPE (i), raw_buffer, 0,
  953.              stdout,   0, 1, 0, Val_pretty_default);
  954.     }
  955.  
  956.       /* The SPARC wants to print even-numbered float regs as doubles
  957.      in addition to printing them as floats.  */
  958. #ifdef PRINT_REGISTER_HOOK
  959.       PRINT_REGISTER_HOOK (i);
  960. #endif
  961.  
  962.       printf_filtered ("\n");
  963.     }
  964. }
  965. #endif /* no DO_REGISTERS_INFO.  */
  966.  
  967. static void
  968. registers_info (addr_exp, fpregs)
  969.      char *addr_exp;
  970.      int fpregs;
  971. {
  972.   int regnum;
  973.  
  974.   if (!target_has_registers)
  975.     error ("The program has no registers now.");
  976.  
  977.   if (addr_exp)
  978.     {
  979.       if (*addr_exp >= '0' && *addr_exp <= '9')
  980.     regnum = atoi (addr_exp);
  981.       else
  982.     {
  983.       register char *p = addr_exp;
  984.       if (p[0] == '$')
  985.         p++;
  986.       for (regnum = 0; regnum < NUM_REGS; regnum++)
  987.         if (!strcmp (p, reg_names[regnum]))
  988.           break;
  989.       if (regnum == NUM_REGS)
  990.         error ("%s: invalid register name.", addr_exp);
  991.     }
  992.     }
  993.   else
  994.     regnum = -1;
  995.  
  996.   DO_REGISTERS_INFO(regnum, fpregs);
  997. }
  998.  
  999. static void
  1000. all_registers_info (addr_exp, from_tty)
  1001.      char *addr_exp;
  1002.      int from_tty;
  1003. {
  1004.   registers_info (addr_exp, 1);
  1005. }
  1006.  
  1007. static void
  1008. nofp_registers_info (addr_exp, from_tty)
  1009.      char *addr_exp;
  1010.      int from_tty;
  1011. {
  1012.   registers_info (addr_exp, 0);
  1013. }
  1014.  
  1015. /*
  1016.  * TODO:
  1017.  * Should save/restore the tty state since it might be that the
  1018.  * program to be debugged was started on this tty and it wants
  1019.  * the tty in some state other than what we want.  If it's running
  1020.  * on another terminal or without a terminal, then saving and
  1021.  * restoring the tty state is a harmless no-op.
  1022.  * This only needs to be done if we are attaching to a process.
  1023.  */
  1024.  
  1025. /*
  1026.  * attach_command --
  1027.  * takes a program started up outside of gdb and ``attaches'' to it.
  1028.  * This stops it cold in its tracks and allows us to start tracing it.
  1029.  * For this to work, we must be able to send the process a
  1030.  * signal and we must have the same effective uid as the program.
  1031.  */
  1032. void
  1033. attach_command (args, from_tty)
  1034.      char *args;
  1035.      int from_tty;
  1036. {
  1037.   dont_repeat ();            /* Not for the faint of heart */
  1038.   target_attach (args, from_tty);
  1039. }
  1040.  
  1041. /*
  1042.  * detach_command --
  1043.  * takes a program previously attached to and detaches it.
  1044.  * The program resumes execution and will no longer stop
  1045.  * on signals, etc.  We better not have left any breakpoints
  1046.  * in the program or it'll die when it hits one.  For this
  1047.  * to work, it may be necessary for the process to have been
  1048.  * previously attached.  It *might* work if the program was
  1049.  * started via the normal ptrace (PTRACE_TRACEME).
  1050.  */
  1051.  
  1052. static void
  1053. detach_command (args, from_tty)
  1054.      char *args;
  1055.      int from_tty;
  1056. {
  1057.   dont_repeat ();            /* Not for the faint of heart */
  1058.   target_detach (args, from_tty);
  1059. }
  1060.  
  1061. /* ARGSUSED */
  1062. static void
  1063. float_info (addr_exp, from_tty)
  1064.      char *addr_exp;
  1065.      int from_tty;
  1066. {
  1067. #ifdef FLOAT_INFO
  1068.     FLOAT_INFO;
  1069. #else
  1070.     printf ("No floating point info available for this processor.\n");
  1071. #endif
  1072. }
  1073.  
  1074. struct cmd_list_element *unsetlist = NULL;
  1075.  
  1076. /* ARGSUSED */
  1077. static void
  1078. unset_command (args, from_tty)
  1079.      char *args;
  1080.      int from_tty;
  1081. {
  1082.   printf ("\"unset\" must be followed by the name of an unset subcommand.\n");
  1083.   help_list (unsetlist, "unset ", -1, stdout);
  1084. }
  1085.  
  1086. void
  1087. _initialize_infcmd ()
  1088. {
  1089.   struct cmd_list_element *c;
  1090.   
  1091.   add_com ("tty", class_run, tty_command,
  1092.        "Set terminal for future runs of program being debugged.");
  1093.  
  1094.   add_show_from_set
  1095.     (add_set_cmd ("args", class_run, var_string_noescape, (char *)&inferior_args,
  1096.           
  1097. "Set arguments to give program being debugged when it is started.\n\
  1098. Follow this command with any number of args, to be passed to the program.",
  1099.           &setlist),
  1100.      &showlist);
  1101.  
  1102.   c = add_cmd
  1103.     ("environment", no_class, environment_info,
  1104.      "The environment to give the program, or one variable's value.\n\
  1105. With an argument VAR, prints the value of environment variable VAR to\n\
  1106. give the program being debugged.  With no arguments, prints the entire\n\
  1107. environment to be given to the program.", &showlist);
  1108.   c->completer = noop_completer;
  1109.  
  1110.   add_prefix_cmd ("unset", no_class, unset_command,
  1111.           "Complement to certain \"set\" commands",
  1112.           &unsetlist, "unset ", 0, &cmdlist);
  1113.   
  1114.   c = add_cmd ("environment", class_run, unset_environment_command,
  1115.           "Cancel environment variable VAR for the program.\n\
  1116. This does not affect the program until the next \"run\" command.",
  1117.        &unsetlist);
  1118.   c->completer = noop_completer;
  1119.  
  1120.   c = add_cmd ("environment", class_run, set_environment_command,
  1121.            "Set environment variable value to give the program.\n\
  1122. Arguments are VAR VALUE where VAR is variable name and VALUE is value.\n\
  1123. VALUES of environment variables are uninterpreted strings.\n\
  1124. This does not affect the program until the next \"run\" command.",
  1125.        &setlist);
  1126.   c->completer = noop_completer;
  1127.  
  1128.   add_com ("path", class_files, path_command,
  1129.        "Add directory DIR(s) to beginning of search path for object files.\n\
  1130. $cwd in the path means the current working directory.\n\
  1131. This path is equivalent to the $PATH shell variable.  It is a list of\n\
  1132. directories, separated by colons.  These directories are searched to find\n\
  1133. fully linked executable files and separately compiled object files as needed.");
  1134.  
  1135.   c = add_cmd ("paths", no_class, path_info,
  1136.         "Current search path for finding object files.\n\
  1137. $cwd in the path means the current working directory.\n\
  1138. This path is equivalent to the $PATH shell variable.  It is a list of\n\
  1139. directories, separated by colons.  These directories are searched to find\n\
  1140. fully linked executable files and separately compiled object files as needed.", &showlist);
  1141.   c->completer = noop_completer;
  1142.  
  1143.  add_com ("attach", class_run, attach_command,
  1144.         "Attach to a process or file outside of GDB.\n\
  1145. This command attaches to another target, of the same type as your last\n\
  1146. `target' command (`info files' will show your target stack).\n\
  1147. The command may take as argument a process id or a device file.\n\
  1148. For a process id, you must have permission to send the process a signal,\n\
  1149. and it must have the same effective uid as the debugger.\n\
  1150. When using \"attach\", you should use the \"file\" command to specify\n\
  1151. the program running in the process, and to load its symbol table.");
  1152.  
  1153.   add_com ("detach", class_run, detach_command,
  1154.        "Detach a process or file previously attached.\n\
  1155. If a process, it is no longer traced, and it continues its execution.  If you\n\
  1156. were debugging a file, the file is closed and gdb no longer accesses it.");
  1157.  
  1158.   add_com ("signal", class_run, signal_command,
  1159.        "Continue program giving it signal number SIGNUMBER.");
  1160.  
  1161.   add_com ("stepi", class_run, stepi_command,
  1162.        "Step one instruction exactly.\n\
  1163. Argument N means do this N times (or till program stops for another reason).");
  1164.   add_com_alias ("si", "stepi", class_alias, 0);
  1165.  
  1166.   add_com ("nexti", class_run, nexti_command,
  1167.        "Step one instruction, but proceed through subroutine calls.\n\
  1168. Argument N means do this N times (or till program stops for another reason).");
  1169.   add_com_alias ("ni", "nexti", class_alias, 0);
  1170.  
  1171.   add_com ("finish", class_run, finish_command,
  1172.        "Execute until selected stack frame returns.\n\
  1173. Upon return, the value returned is printed and put in the value history.");
  1174.  
  1175.   add_com ("next", class_run, next_command,
  1176.        "Step program, proceeding through subroutine calls.\n\
  1177. Like the \"step\" command as long as subroutine calls do not happen;\n\
  1178. when they do, the call is treated as one instruction.\n\
  1179. Argument N means do this N times (or till program stops for another reason).");
  1180.   add_com_alias ("n", "next", class_run, 1);
  1181.  
  1182.   add_com ("step", class_run, step_command,
  1183.        "Step program until it reaches a different source line.\n\
  1184. Argument N means do this N times (or till program stops for another reason).");
  1185.   add_com_alias ("s", "step", class_run, 1);
  1186.  
  1187.   add_com ("until", class_run, until_command,
  1188.        "Execute until the program reaches a source line greater than the current\n\
  1189. or a specified line or address or function (same args as break command).\n\
  1190. Execution will also stop upon exit from the current stack frame.");
  1191.   add_com_alias ("u", "until", class_run, 1);
  1192.   
  1193.   add_com ("jump", class_run, jump_command,
  1194.        "Continue program being debugged at specified line or address.\n\
  1195. Give as argument either LINENUM or *ADDR, where ADDR is an expression\n\
  1196. for an address to start at.");
  1197.  
  1198.   add_com ("continue", class_run, continue_command,
  1199.        "Continue program being debugged, after signal or breakpoint.\n\
  1200. If proceeding from breakpoint, a number N may be used as an argument:\n\
  1201. then the same breakpoint won't break until the Nth time it is reached.");
  1202.   add_com_alias ("c", "cont", class_run, 1);
  1203.   add_com_alias ("fg", "cont", class_run, 1);
  1204.  
  1205.   add_com ("run", class_run, run_command,
  1206.        "Start debugged program.  You may specify arguments to give it.\n\
  1207. Args may include \"*\", or \"[...]\"; they are expanded using \"sh\".\n\
  1208. Input and output redirection with \">\", \"<\", or \">>\" are also allowed.\n\n\
  1209. With no arguments, uses arguments last specified (with \"run\" or \"set args\".\n\
  1210. To cancel previous arguments and run with no arguments,\n\
  1211. use \"set args\" without arguments.");
  1212.   add_com_alias ("r", "run", class_run, 1);
  1213.  
  1214.   add_info ("registers", nofp_registers_info,
  1215.     "List of integer registers and their contents, for selected stack frame.\n\
  1216. Register name as argument means describe only that register.");
  1217.  
  1218.   add_info ("all-registers", all_registers_info,
  1219. "List of all registers and their contents, for selected stack frame.\n\
  1220. Register name as argument means describe only that register.");
  1221.  
  1222.   add_info ("program", program_info,
  1223.         "Execution status of the program.");
  1224.  
  1225.   add_info ("float", float_info,
  1226.         "Print the status of the floating point unit\n");
  1227.  
  1228.   inferior_args = savestring ("", 1);    /* Initially no args */
  1229.   inferior_environ = make_environ ();
  1230.   init_environ (inferior_environ);
  1231. }
  1232.